﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Script.UI;
using UnityEngine.UI;
using XCharts;
using Script.UI.Component;
using LitJson;
using MTUnity.Utils;
using System;
public class UI_Overview : UIUpdateBase
{
    public UI_AirPressure Panel_AirPressure;
    public UI_Voltage Panel_Voltage;
    public UI_Visitors Panel_Visitors;

    private FLocation m_fCurrentLocation;

    void Start()
    {
        GlobalEvent.AddEvent(EventName.CurrentVisitorUpdate, OnCurrentVisitorUpdate);
        GlobalEvent.AddEvent(EventName.CurrentAirPressureUpdate, OnCurrentAirPressureUpdate);
        GlobalEvent.AddEvent(EventName.CurrentVoltageUpdate, OnCurrentVoltageUpdate);

        GlobalEvent.AddEvent(EventName.VisitorRefreshTimeUpdate, OnVisitorRefreshTimeUpdate);
        GlobalEvent.AddEvent(EventName.AirPressureRefreshTimeUpdate, OnAirPressureRefreshTimeUpdate);
        GlobalEvent.AddEvent(EventName.VoltageRefreshTimeUpdate, OnVoltageRefreshTimeUpdate);

        GlobalEvent.AddEvent(EventName.RecordVisitorUpdate, OnRecordVisitorUpdate);
        GlobalEvent.AddEvent(EventName.RecordAirPressureUpdate, OnRecordAirPressureUpdate);
        GlobalEvent.AddEvent(EventName.RecordVoltageUpdate,     OnRecordVoltageUpdate);

    }

	#region Event
    public void OnCurrentVisitorUpdate(object param)
    {
        List<string> listGatherKey = new List<string>
        {
            JsonFildName.OverView.Current_Visitor,
        };
        JsonData gatherData = DataGather(listGatherKey);

        DataProcessor(gatherData);
    }

    public void OnCurrentAirPressureUpdate(object param)
    {
        List<string> listGatherKey = new List<string>
        {
            JsonFildName.OverView.Current_AirPressure,
        };
        JsonData gatherData = DataGather(listGatherKey);

        DataProcessor(gatherData);
    }

    public void OnCurrentVoltageUpdate(object param)
    {
        List<string> listGatherKey = new List<string>
        {
            JsonFildName.OverView.Current_Voltage,
        };
        JsonData gatherData = DataGather(listGatherKey);

        DataProcessor(gatherData);
    }

    public void OnVisitorRefreshTimeUpdate(object param)
    {
        List<string> listGatherKey = new List<string>
        {
            JsonFildName.OverView.VisitorRefreshTime,
        };
        JsonData gatherData = DataGather(listGatherKey);

        DataProcessor(gatherData);
    }

    public void OnAirPressureRefreshTimeUpdate(object param)
    {
        List<string> listGatherKey = new List<string>
        {
            JsonFildName.OverView.AirPressureRefreshTime,
        };
        JsonData gatherData = DataGather(listGatherKey);

        DataProcessor(gatherData);
    }

    public void OnVoltageRefreshTimeUpdate(object param)
    {
        List<string> listGatherKey = new List<string>
        {
            JsonFildName.OverView.VoltageRefreshTime,
        };
        JsonData gatherData = DataGather(listGatherKey);

        DataProcessor(gatherData);
    }

    public void OnRecordVisitorUpdate(object param)
    {
        List<string> listGatherKey = new List<string>
        {
            JsonFildName.OverView.Record_Visitor,
        };
        JsonData gatherData = DataGather(listGatherKey);

        DataProcessor(gatherData);
    }

    public void OnRecordAirPressureUpdate(object param)
    {
        List<string> listGatherKey = new List<string>
        {
            JsonFildName.OverView.Record_AirPressure,
        };
        JsonData gatherData = DataGather(listGatherKey);

        DataProcessor(gatherData);
    }

    public void OnRecordVoltageUpdate(object param)
    {
        List<string> listGatherKey = new List<string>
        {
            JsonFildName.OverView.Record_Voltage,
        };
        JsonData gatherData = DataGather(listGatherKey);

        DataProcessor(gatherData);
    }
    #endregion

    #region Interface
    public override Dictionary<string, IUIUpdateInterface> RegisterProcessorObj()
    {
        return new Dictionary<string, IUIUpdateInterface>
        {
            {JsonFildName.OverView.Visitor , Panel_Visitors },
            {JsonFildName.OverView.Voltage , Panel_Voltage },
            {JsonFildName.OverView.AirPressure , Panel_AirPressure },
        };
    }
    public override Dictionary<string, GatherDel> RegisterGatherMethod()
    {
        return new Dictionary<string, GatherDel>
        {
            //current
            { JsonFildName.OverView.Current_Visitor , GatherCurrentVisitor},
            { JsonFildName.OverView.Current_AirPressure , GatherCurrentAirPressure},
            { JsonFildName.OverView.Current_Voltage , GatherCurrentVoltage},
            //record
            { JsonFildName.OverView.Record_Visitor , GatherRecordVisitor},
            { JsonFildName.OverView.Record_AirPressure , GatherRecordAirPressure},
            { JsonFildName.OverView.Record_Voltage , GatherRecordVoltage},
            //refresh
            { JsonFildName.OverView.VisitorRefreshTime , GatherVisitorRefreshTime},
            { JsonFildName.OverView.AirPressureRefreshTime , GatherAirPressureRefreshTime},
            { JsonFildName.OverView.VoltageRefreshTime , GatherVoltageRefreshTime},
        };
    }
	#endregion

	#region UpdateInterface

	#endregion

	#region GatherInterface
    public JsonData GatherCurrentVisitor()
    {
        JsonData jsonData = null;
        JsonUtils.GenerateJsonDataWithFieldName(
            ref jsonData,
            JsonFildName.OverView.VisitorCount,
            ((FOverView)m_fCurrentLocation.Data).CurrentVisitor.GetVisitorType(FCurrentVisitor.EVisitorType.E_Total)
            );
        JsonUtils.GenerateJsonDataWithFieldName(
            ref jsonData,
            JsonFildName.OverView.GuestVisitorCount,
            ((FOverView)m_fCurrentLocation.Data).CurrentVisitor.GetVisitorType(FCurrentVisitor.EVisitorType.E_Guest)
            );
        JsonUtils.GenerateJsonDataWithFieldName(
            ref jsonData,
            JsonFildName.OverView.StaffVisitorCount,
            ((FOverView)m_fCurrentLocation.Data).CurrentVisitor.GetVisitorType(FCurrentVisitor.EVisitorType.E_Staff)
            );
        JsonUtils.GenerateJsonDataWithFieldName(
            ref jsonData,
            JsonFildName.OverView.CarVisitorCount,
            ((FOverView)m_fCurrentLocation.Data).CurrentVisitor.GetVisitorType(FCurrentVisitor.EVisitorType.E_Car)
            );
        return jsonData;
    }

    public JsonData GatherCurrentAirPressure()
    {
        return JsonUtils.GenerateJsonData(((FOverView)m_fCurrentLocation.Data).CurrentAirPressure);
    }

    public JsonData GatherCurrentVoltage()
    {
        return JsonUtils.GenerateJsonData(((FOverView)m_fCurrentLocation.Data).CurrentVoltage);
    }

    public JsonData GatherRecordVisitor()
    {
        List<FVisitorRecord> listNearstData = ((FOverView)m_fCurrentLocation.Data).GetNearestVisitorRecordData(14);
        return JsonUtils.GenerateJsonData(listNearstData);
    }

    public JsonData GatherRecordAirPressure()
    {
        List<FAirPressureRecord> listNearstData = ((FOverView)m_fCurrentLocation.Data).GetNearestAirPressureRecordData(12);
        return JsonUtils.GenerateJsonData(listNearstData);
    }

    public JsonData GatherRecordVoltage()
    {
        List<FVoltageRecord> listNearstData = ((FOverView)m_fCurrentLocation.Data).GetNearestVoltageRecordData(12);
        return JsonUtils.GenerateJsonData(listNearstData);
    }

    public JsonData GatherVisitorRefreshTime()
    {
        return JsonUtils.GenerateJsonData(((FOverView)m_fCurrentLocation.Data).GetRefreshDataTime(JsonFildName.OverView.VisitorRefreshTime));
    }
    public JsonData GatherAirPressureRefreshTime()
    {
        return JsonUtils.GenerateJsonData(((FOverView)m_fCurrentLocation.Data).GetRefreshDataTime(JsonFildName.OverView.AirPressureRefreshTime));
    }
    public JsonData GatherVoltageRefreshTime()
    {
        return JsonUtils.GenerateJsonData(((FOverView)m_fCurrentLocation.Data).GetRefreshDataTime(JsonFildName.OverView.VoltageRefreshTime));
    }
    #endregion


    public void SetData(FLocation location)
    {
        m_fCurrentLocation = location;

        List<string> listGatherKey = new List<string>
        {
            JsonFildName.OverView.Current_Visitor,
            JsonFildName.OverView.Current_AirPressure,
            JsonFildName.OverView.Current_Voltage,
            JsonFildName.OverView.Record_Visitor,
            JsonFildName.OverView.Record_AirPressure,
            JsonFildName.OverView.Record_Voltage,
            JsonFildName.OverView.VisitorRefreshTime,
            JsonFildName.OverView.AirPressureRefreshTime,
            JsonFildName.OverView.VoltageRefreshTime,
        };
        JsonData gatherData = DataGather(listGatherKey);

        DataProcessor(gatherData);
    }
}